const app = getApp()
const api = require('../fetch/api')
const { baseURL,ip } = require('../fetch/http')
Component({
  pageLifetimes: {
    show() {
      if (typeof this.getTabBar === 'function' &&
        this.getTabBar()) {
        this.getTabBar().setData({
          selected: 3
        })
      }

      // 获取分享页面的用户id参数 用于注册生成上下级关系
      const pages = getCurrentPages()
      const currentPage = pages[pages.length-1]
      let { id, scene } = currentPage.options
      console.log(currentPage.options);
      if(id) {
        wx.setStorageSync('recommend', id)
      }
      if(scene) {
        wx.setStorageSync('recommend', scene)
      }

      app.authTo(()=>{
        this.userinfo()
        this.orderNum()
      })
    },
  },
  lifetimes: {
    attached() {

    }
  },
  data: {
    baseURL: baseURL,
    ip: ip,
    show: false,
    headimg:'',
    nickname:'',
    balance:'', // 积分
    commission:'', // 红包
    fund:'', // 佣金
    poster: ''
  },  
  methods: {
    ruleDetail() {
      wx.navigateTo({
        url: '/pages/my/ruleDetail/index',
      })
    },
    async userinfo() {
      let res = await api.userinfo()
      let { headimg,nickname,balance,commission,fund,levelName,integral } = res.data
      this.setData({
        headimg,nickname,balance,commission,fund,levelName,integral
      })
    },
    async orderNum() {
      let res = await api.orderNum()
      let { finish,noDelivery,noPay,noSignFor } = res.data
      this.setData({
        finish,noDelivery,noPay,noSignFor
      })
    },
    integral(e) {
      app.authTo(()=>{
        const { type } = app.dataSet(e)
        wx.navigateTo({
          url: '/pages/my/integral/index?type='+type,
        })
      })
    },
    address() {
      app.authTo(()=>{
        wx.navigateTo({
          url: '/pages/my/address/index',
        })
      })
    },
    orders(e) {
      app.authTo(()=>{
        const { index } = app.dataSet(e)
        wx.navigateTo({
          url: '/pages/my/orders/index?index=' + index,
        })
      })
    },
    share() {
      app.authTo(()=>{
        api.poster().then(res=>{
          this.setData({
            poster: res.data
          })
          this.open()
        })
      })
    },
    team() {
      app.authTo(()=>{
        wx.navigateTo({
          url: '/pages/my/team/index',
        })
      })
    },
    redPacket() {
      app.authTo(()=>{
        wx.navigateTo({
          url: '/pages/my/redPacket/index',
        })
      })
    },
    commission() {
      app.authTo(()=>{
        wx.navigateTo({
          url: '/pages/my/commission/index',
        })
      })
    },
    about() {
      wx.navigateTo({
        url: '/pages/my/about/index',
      })
    },
    rule() {
      wx.navigateTo({
        url: '/pages/my/rule/index',
      })
    },
    suggestion() {
      app.authTo(()=>{
        wx.navigateTo({
          url: '/pages/my/suggestion/index',
        })
      })
    },
    saveImage() {
      this.close()
      wx.getImageInfo({
        src: this.data.poster,
        success (res) {
          wx.saveImageToPhotosAlbum({
            filePath: res.path,
            success(res) {
              app.toast('保存成功','success')
            }
          })
        }
      })
    },
    open() {
      this.setData({
        show: true
      })
    },
    close() {
      this.setData({
        show: false
      })
    },
    onShareAppMessage: function (res) {
      if (!wx.getStorageSync('token')) {
        app.toast('请登录')
        return false
      }
      if (res.from === 'button') {
        // 来自页面内转发按钮
        this.close()
        console.log(res.target)
      }
      return {
        imageUrl: this.data.ip + '/static/wx/img/share.png',
        title: '金盾二茅',
        path: 'index/index2?id=' + wx.getStorageSync('id') 
      }
    },
  }
})